package net.huashitong.supervision.AppNew.appCtrl;

import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.AppNew.DTO.UserDTO;
import net.huashitong.supervision.AppNew.appService.AppUserService;
import net.huashitong.supervision.task.filter.TaskUnitFilter;
import net.huashitong.supervision.task.service.TaskUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/app/user")
public class AppUserCtrl extends JsonBaseController {


    @Autowired
    private AppUserService appUserService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysLoginService loginService;

    @Override
    protected String getView(String s) {
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/bytel",method = RequestMethod.GET)
    public Message getUserByPhone(String telphone)
    {

        List<UserDTO> userByPhone = appUserService.getUserByPhone(telphone);


        return coverMessage("200","手机号获取用户调用成功",userByPhone);

    }

    /**
     * 保存登陆者信息
     * @param userDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveuser",method = RequestMethod.POST)
    public Message getUserByPhone(@RequestBody UserDTO userDTO)
    {
//        String userId1 = userDTO.getUserId();

        //  List<UserDTO> userByPhone = appUserService.getUserByPhone(telphone);
        String userId = userDTO.getUserId();
        if(StringUtils.isBlank(userId))
        {
            return  coverMessage("500","用户id不能为空");
        }

        SysUser sysUser = userService.getById(userId);
        SysLogin login = loginService.getById(userId);
        UserDTO dto = new UserDTO();
        dto.setUsername(login.getUsername());
        dto.setDeptId(login.getDeptId());
        dto.setRoleId(login.getRoleId());
        dto.setMobile(sysUser.getMobile());
        dto.setRealName(sysUser.getName());
        dto.setUserId(login.getSysId());
        dto.setPersonSignal(sysUser.getPersonSignal());
        dto.setState(login.getState());
        dto.setUserType(login.getUserType());
        String s = JSONObject.toJSONString(dto);
        redisService.del("applogin:dc:"+userId);
        redisService.set("applogin:dc:"+userId,s);

        return coverMessage("200","保存成功",s);

    }

    /**
     * 根据单位任务获取批转人
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/endorseList", method = RequestMethod.POST)
    public Message endorseList(@RequestBody TaskUnitFilter filter) {
        String userId = filter.getUserId();
        String taskUnitId = filter.getSysId();
        List<SysUser> userList = taskUnitService.queryEndorseList(taskUnitId, userId);
        if (ObjectUtils.isBlank(userList)) {
            return coverMessage("500");
        } else {
            return coverMessage("200", "success", userList);
        }
    }

}
